<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>For Each</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="com.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="com.examples.arrays.html">Arrays and Array-style COM properties</a></div>
 <div class="up"><a href="com.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="com.examples.foreach" class="section">
  <h2 class="title">For Each</h2>
  <p class="para">
   Starting with PHP 5, you may use PHP&#039;s own 
   <a href="control-structures.foreach.html" class="link">foreach</a> statement to iterate
   over the contents of a standard COM/OLE IEnumVariant.  In layman&#039;s terms,
   this means that you can use foreach in places where you would have used
   <em>For Each</em> in VB/ASP code.
  </p>
  <p class="para">
   <div class="example" id="example-5233">
    <p><strong>Example #1 For Each in ASP</strong></p>
    <div class="example-contents">
<div class="aspcode"><pre class="aspcode">&lt;%
Set domainObject = GetObject(&quot;WinNT://Domain&quot;)
For Each obj in domainObject
  Response.Write obj.Name &amp; &quot;&lt;br /&gt;&quot;
Next
%&gt;</pre>
</div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5234">
    <p><strong>Example #2 while() ... Next() in PHP 4</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;<br />$domainObject&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"WinNT://Domain"</span><span style="color: #007700">);&nbsp;<br />while&nbsp;(</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$domainObject</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Next</span><span style="color: #007700">())&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5235">
    <p><strong>Example #3 foreach in PHP 5</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;<br />$domainObject&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">COM</span><span style="color: #007700">(</span><span style="color: #DD0000">"WinNT://Domain"</span><span style="color: #007700">);&nbsp;<br />foreach&nbsp;(</span><span style="color: #0000BB">$domainObject&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;&nbsp;<br />}&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="com.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="com.examples.arrays.html">Arrays and Array-style COM properties</a></div>
 <div class="up"><a href="com.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
